﻿using UnityEngine;

namespace MiHoMiao.Mono.Singleton
{
    public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T m_Instance;

        public static T Instance
        {
            get
            {
                if (m_Instance != null) return m_Instance;
                m_Instance = FindFirstObjectByType<T>();
                return !m_Instance ? null : m_Instance;
            }
        }

        protected void Awake()
        {
            if (m_Instance != null) return;
            OnAwake();
            if (DestroyOnLoad) return;
            DontDestroyOnLoad(this);
        }

        protected virtual void OnAwake()
        {
        }
        
        protected virtual bool DestroyOnLoad => true;

    }

}